/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jhu.profiteer.data;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jhu.profiteer.data.exceptions.NonexistentEntityException;

/**
 *
 * @author Jeremy
 */
public class GameConfigurationParametersJpaController {

  public GameConfigurationParametersJpaController() {
    emf = Persistence.createEntityManagerFactory("profiteer-gamePU");
  }
  private EntityManagerFactory emf = null;

  public EntityManager getEntityManager() {
    return emf.createEntityManager();
  }

  public void create(GameConfigurationParameters gameConfigurationParameters) {
    EntityManager em = null;
    try {
      em = getEntityManager();
      em.getTransaction().begin();
      em.persist(gameConfigurationParameters);
      em.getTransaction().commit();
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public void edit(GameConfigurationParameters gameConfigurationParameters)
          throws NonexistentEntityException, Exception {
    EntityManager em = null;
    try {
      em = getEntityManager();
      em.getTransaction().begin();
      gameConfigurationParameters = em.merge(gameConfigurationParameters);
      em.getTransaction().commit();
    } finally {
      if (em != null) {
        em.close();
      }
    }
  }

  public GameConfigurationParameters findGameConfigurationParameters() {
    EntityManager em = getEntityManager();
    try {
      List<GameConfigurationParameters> l = (List<GameConfigurationParameters>)
              em.createQuery("select object(o) from" +
              " GameConfigurationParameters as o").getResultList();
      if(1 == l.size()) return l.get(0);
      else return null;
    } finally {
      em.close();
    }
  }
}
